package com.just521.xinshoutong;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.graphics.drawable.AnimationDrawable;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;

import com.alibaba.fastjson.JSON;
import com.feedback.NotificationType;
import com.feedback.UMFeedbackService;
import com.just521.xinshoutong.common.CommonDefine;
import com.just521.xinshoutong.database.NoteDBCtrl;
import com.just521.xinshoutong.dialog.CustomDlg;
import com.just521.xinshoutong.http.HttpHelperWraper;
import com.mobclick.android.MobclickAgent;
class User { 
 private Long id; 
 private String name;

 public Long getId() { return id; } 
 public void setId(Long id) { this.id = id; }

 public String getName() { return name; } 
 public void setName(String name) { this.name = name; } 
} 
class User2 { 
	 private Long id; 
	 private String name;

	 public Long getId() { return id; } 
	 public void setId(Long id) { this.id = id; }

	 public String getName() { return name; } 
	 public void setName(String name) { this.name = name; } 
	} 
class Group { 
 private Long id; 
 private String name; 
 private List<Object> users = new ArrayList<Object>();

 public Long getId() { return id; } 
 public void setId(Long id) { this.id = id; }

 public String getName() { return name; } 
 public void setName(String name) { this.name = name; }

 public List<Object> getUsers() { return users; } 
 public void setUsers(List<Object> users) { this.users = users; } 
}   
public class Splash extends ActivityBase {    
  
    private AnimationDrawable draw = null;  
    
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
    	requestWindowFeature(Window.FEATURE_NO_TITLE);
    	super.onCreate(savedInstanceState); 
        setContentView(R.layout.splash); 
        reciver = true;
        
    	MobclickAgent.onError(this);
    	MobclickAgent.updateOnlineConfig(this);
    	UMFeedbackService.enableNewReplyNotification(this, NotificationType.NotificationBar);
    	
    	Group group = new Group(); 
    	group.setId(0L); 
    	group.setName("admin");

    	User guestUser = new User(); 
    	guestUser.setId(2L); 
    	guestUser.setName("guest");

    	User rootUser = new User(); 
    	rootUser.setId(3L); 
    	rootUser.setName("root");
    	
    	User2 u2 = new User2(); 
    	u2.setId(3L); 
    	u2.setName("22");

    	group.getUsers().add(guestUser); 
    	group.getUsers().add(u2);
    	group.getUsers().add(rootUser);
    	

    	String jsonString = JSON.toJSONString(group);
    	Group temp=JSON.parseObject(jsonString, Group.class);
    	String nativestring=new NoteDBCtrl().stringFromJNI();
    	
    	HttpHelperWraper.init(this);
    	CommonDefine.gappctx=getApplicationContext();
    	try{
    		PackageInfo pinfo = getPackageManager().getPackageInfo(getPackageName(), PackageManager.GET_CONFIGURATIONS);
    		CommonDefine.gPackageVer = String.valueOf(pinfo.versionCode);
    		
    	}catch(NameNotFoundException e){
    		
    	}
    	
    	DisplayMetrics dm = new DisplayMetrics();  
    	dm = getResources().getDisplayMetrics();  
    	CommonDefine.Screen.density = dm.density;
    	CommonDefine.Screen.screen_h = dm.heightPixels;
    	CommonDefine.Screen.screen_w = dm.widthPixels;

    	save_Chanel();
    	CheckNetwork();  
    	
    	startActivity(new Intent(this,Activity_main_notelist.class));
    } 

    
    
    

	
	@Override
	public void onWindowFocusChanged( boolean hasFocus )
	{
		super.onWindowFocusChanged(hasFocus);
    	ImageView view = (ImageView)findViewById(R.id.luckyload);  
    	if(view != null)
    	{
        	draw = (AnimationDrawable)view.getDrawable(); 
    		if(draw != null)  draw.start();    		
    	}

		
	}
	
	private boolean CheckNetwork() {
		
	    boolean flag = false;
	    ConnectivityManager cwjManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
	    if (cwjManager.getActiveNetworkInfo() != null)
	        flag = cwjManager.getActiveNetworkInfo().isAvailable();
	    if (!flag)
	    {
			final CustomDlg dlg = new CustomDlg(Splash.this, R.style.mydialog);
			dlg.setDisplayView( null,"网络连接失败，请检查网络", "设置网络","退出");
			dlg.setLBtnListner(new View.OnClickListener() {			
				@Override
				public void onClick(View arg0) {
					dlg.cancel();
	    			Intent mIntent = new Intent();
	                mIntent.setAction("android.settings.WIRELESS_SETTINGS");
	                startActivity(mIntent);
	                Splash.this.finish();
				}
			});
			
			dlg.setRBtnListner(new View.OnClickListener() {			
				@Override
				public void onClick(View arg0) {
					dlg.cancel();
	    			Splash.this.finish();
				}
			});		

			if(!this.isFinishing()) dlg.show();	
	    	
	    	
	    	
	    	    	
	    }
	    else
	    {
	        
	    }
	   
		return flag;
	
	}
	
	 void save_Chanel()
	{
		 String channel = ManifestMetaData.getString(this, "UMENG_CHANNEL");
		 CommonDefine.gSoftChannel = ManifestMetaData.getString(this, "channelid");
		 
        SharedPreferences MyPreferences = getSharedPreferences("channel", Activity.MODE_PRIVATE);
        String str = MyPreferences.getString("curchannel", "");
        if(!str.equals(channel)&&!channel.equals("trunk-release-waps")){
        	SharedPreferences.Editor editor = MyPreferences.edit();
        	editor.putString("curchannel", channel);
        	Log.v("Debug_lidan", "save channel:"+channel);
        	editor.commit();
        }else{
        	Log.v("Debug_lidan", "save channel:no");
        }
	}
	 
	 
	 
	 
	 
	/*------------------------------------------------------------------------------------*/ 
	public static class ManifestMetaData {

		private static Object readKey(Context context, String keyName) {

			try {
				ApplicationInfo appi =  context.getPackageManager().getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA);
				Bundle bundle = appi.metaData;
				Object value = bundle.get(keyName);
				return value;
			} catch (NameNotFoundException e) {
				return null;
			}
		}

		public static int getInt(Context context, String keyName) {
			return (Integer)readKey(context, keyName);
		}


		public static String getString(Context context, String keyName ) {
			return (String ) readKey(context, keyName);
		}


		public static Boolean getBoolean(Context context, String keyName) {
			return (Boolean) readKey(context, keyName);
		}

		public static Object get(Context context, String keyName) {
			return readKey(context, keyName);
		}

}
	
	
    
	
    
}